<?php
/**
 * haibaofocuscontroller.php
 * 
 * Create on 2015-04-01 17:18:43
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\index;

use haibao\cms\business\control\PublicController;
use haibao\cms\business\control\pc\entity\ArticleEntity;
use haibao\cms\business\control\pc\action\ArticleAction;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;

class RightNowController extends IndexController{
	
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$path = $this->generateHtmlPath(__CLASS__);
		$url = $title = $href = '';
		if($controlData['auto']){
			$temp = array();
			foreach($controlData['auto'] as $data){
				$parameterValue = json_decode($data['ParameterValue'],true);
				$url = $parameterValue['url'];
				$title = $parameterValue['title'];
				$href = $parameterValue['href'];
			}
		}
		$articleList = array();
		if($controlData['manual']){
			$articleList = $this->getActualData($controlData['manual']);
			$this->assign('articleList', $articleList);
		}
		
		// 获取产品试用
		$tryProducts = $this->getTryProduct();
		$this->assign('tryProducts', $tryProducts);
		
		$this->assign('url', $url);
		$this->assign('title', $title);
		$this->assign('href', $href);
		$this->assign('tryHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_TRY));
		$isNewHtml = $this->generateHtml($path,$content);

		$content = $this->getHtml($path);
		// 解析内容 更换链接
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	/**
	 * 获取实时模版数据
	 * @param : $dataCondition 数据条件
	 */
	public function getActualData($dataCondition){
	    if(!isset($dataCondition['level'])){
	        $dataCondition['level'] = 6;
	    }
	    if(!isset($dataCondition['biglevel'])){
	        $dataCondition['biglevel'] = 6;
	    }
		$articleEntity = new ArticleEntity();
		$articleEntity->setLevel($dataCondition['level']);
		$articleEntity->setBigLevel($dataCondition['biglevel']);
		$articleEntity->setLimit(6);
		$articleEntity->setIsProImage(false);
		$articleEntity->setIsLeftJoinTag(true);
		$articleEntity->setIsPrimaryTag(true);
		$articleEntity->setIsReturnTag(true);
		$articleEntity->setIsImageColl(true);
		$articleEntity->setSource(__CLASS__);
		$articleEntity->setIsDebug(false);
		$articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FOUR);

		$this->articleAction->setArticleEnity($articleEntity);
		list($articles,$imageColls) = $this->articleAction->getListForNew();

		$mergeData = $this->mergeData($articles['list'],$imageColls['list'],6);
		$articleList = array();
		foreach($mergeData as $key=>$article){
			
			$articleArr = array();
			$article->PublishTime = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
			$articleArr = $article->toArray();
			$articleArr['image'] = array();
			if($article->SiteType == ArticleAction::INFO_TYPE_ARTICLE){
				$tag = $articles['tag'];
				$articleArr['TagUrl'] = $tag[$article->ArticleTag->TagId]['Url'];
				$articleArr['TagName'] = $tag[$article->ArticleTag->TagId]['TagName'];
				$articleArr['Channel'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId);
				$articleArr['ArticlePicUrl'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId,ArticleAction::INFO_TYPE_ARTICLE_PIC);
				$imageByArticle = PublicController::getImagesByArticleId($article->Id,\haibao\cms\model\data\ArticleProImage::Level_FOUR,\haibao\cms\business\control\Base::DATA_TYPE_ARTICLE);
				foreach ($imageByArticle as $image){
					array_push($articleArr['image'], $image);
				}

			}else{
				$tag = $imageColls['tag'];
				$articleArr['TagUrl'] = $tag[$article->ImageCollectiveTag->TagId]['Url'];
				$articleArr['TagName'] = $tag[$article->ImageCollectiveTag->TagId]['TagName'];
				$articleArr['Channel'] = self::getArticleUrl($article->Id,$article->ImageCollectiveTag->ParentId,ArticleAction::INFO_TYPE_IMAGE);
				$articleArr['SiteType'] = $article->SiteType;
				$imageByArticle = PublicController::getImagesByArticleId($article->Id,\haibao\cms\model\data\ArticleProImage::Level_FOUR,\haibao\cms\business\control\Base::DATA_TYPE_IMAGE_SET);
				foreach ($imageByArticle as $image){
					array_push($articleArr['image'], $image);
				}
			}
			array_push($articleList,$articleArr);
		}
		$articleList =  json_decode(json_encode($articleList));
		return $articleList;
	}
	
	public function getTryProduct(){
	    return  PublicControllerBusiness::getTryProduct(10);
	}
	
}